package cn.imvc.app.controllers.goods;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;

import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsImage;
import cn.imvc.app.entities.GoodsSpecValue;
import cn.imvc.app.models.GoodsImageModel;
import cn.imvc.app.models.GoodsSpecValueModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class GoodsImageControllerImpl implements GoodsImageController {
	static Logger logger = Logger.getLogger(GoodsImageControllerImpl.class);
	@Autowired
	GoodsImageModel goodsImageModel;
	@Autowired
	GoodsSpecValueModel goodsSpecValueModel;
	@Autowired
	LoggingModel loggingModel;
	
	final int goodsImageCount = 6;
	public Page<GoodsImage> page;
	public GoodsImage goodsImage;
	public int goodsSpecClassId;
	public int goodsId;
	public int id;
	public int[] ids;
	public ViewRequest viewRequest;
	public List<GoodsImage> goodsImages;
	
	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public void setGoodsImage(GoodsImage goodsImage) {
		this.goodsImage = goodsImage;
	}
	
	public void setGoodsSpecClassId(int goodsSpecClassId) {
		this.goodsSpecClassId = goodsSpecClassId;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public void setGoodsImages(List<GoodsImage> goodsImages) {
		this.goodsImages = goodsImages;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}
	
	public String search() {
		// TODO Auto-generated method stub
		return null;
	}

	public String index() {
		goodsImages = new ArrayList<GoodsImage>();
		Map<String, String> searchMap = new HashMap<String, String>();
		if(goodsSpecClassId == 0){
			searchMap.put("goodsSpecValueId_boolean", String.valueOf(" is null"));
		}else{
			searchMap.put("goodsSpecValueId_eq_string", String.valueOf(goodsSpecClassId));
		}
		searchMap.put(" _and_goodsId_eq_string", String.valueOf(goodsId));
		viewRequest = new ViewRequest();
		viewRequest.setParams(searchMap);
		page=goodsImageModel.find(viewRequest);
//		if(page == null){
//			page = new PageImpl<GoodsImage>(new ArrayList<GoodsImage>());
//		}
//		goodsImages.addAll(page.getContent());
//		for(int i=page.getContent().size()+1;i<=goodsImageCount;i++){
//			GoodsImage goodsImageTmp = new GoodsImage();
//			Goods goodsTmp = new Goods();
//			goodsTmp.setId(goodsId);
//			GoodsSpecValue goodsSpecValue = new GoodsSpecValue();
//			goodsSpecValue.setId(goodsSpecClassId);
//			goodsImageTmp.setGoods(goodsTmp);
//			goodsImageTmp.setGoodsSpecValue(goodsSpecValue);
//			goodsImages.add(goodsImageTmp);
//		}
		
		return "goodsImage/index";
	}

	public String show() {
		// TODO Auto-generated method stub
		return null;
	}

	public String editNew() {
		goodsImage = new GoodsImage();
		Goods goodsTmp = new Goods();
		goodsTmp.setId(goodsId);
		GoodsSpecValue goodsSpecValue = new GoodsSpecValue();
		goodsSpecValue.setId(goodsSpecClassId);
		goodsImage.setGoods(goodsTmp);
		goodsImage.setGoodsSpecValue(goodsSpecValue);
		return "goodsImage/edit";
	}

	public String create() {
		goodsImageModel.save(goodsImage);
		loggingModel.info(logger, "添加图片："+goodsImage.getImgurl());
		return Render.json(new DialogBean("200", "创建成功","goods_image_index", "closeCurrent"));
	}

	public String edit() {
		goodsImage = goodsImageModel.get(id);
		return "goodsImage/edit";
	}

	public String update() {
		goodsImageModel.update(goodsImage);
		loggingModel.info(logger, "编辑图片："+goodsImage.getImgurl());
		return Render.json(new DialogBean("200", "更新成功","goods_image_index", "closeCurrent"));
	}

	public String destroy() {
		goodsImageModel.delete(id);
		loggingModel.info(logger, "删除图片："+id);
		return Render.json(new DialogBean("200", "删除成功","goods_image_index"));
	}

	public String destroyBatch() {
		goodsImageModel.deleteBatch(ids);
		return Render.json(new DialogBean("200", "删除成功","goods_image_index"));
	}
}
